<?php

/*
 * cui:phpstorm
 * 2021/7/24
 * 16:48
 * User: s1mple_qcloud@163.com
 *
 */

namespace App\Api\Controllers;


use App\Api\Services\Goods;
use App\Api\Services\Coupon;
use App\Api\Services\JsonService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


class CouponController extends Controller
{
    /**
     * 前台展示我的优惠券列表
     * @param Request $request
     * @return array
     */
    public function getUserCoupon(Request $request)
    {
        $id = (int)$request->get('id');
        if (!$id || !is_numeric($id)) return JsonService::fail('系统繁忙');
        $data = Coupon::getUserCoupon($id);
        if ($data){
            return JsonService::success('查询成功',$data,config('code.HTTP_OK'));
        }else{
            return JsonService::fail('查询失败','',config('code.HTTP_FAIL'));
        }
    }

    /**
     * 根据商品ID查询该商品所有的优惠券
     * @param Request $request
     */
    public function getListGoodsCoupon(Request $request)
    {
        // 获取商品id
        $id = $request->get('goods_id');
        // 用户id
        $memberId = $request->get('member_id');
        if (!$id || !is_numeric($id)) return JsonService::fail('系统繁忙','',config('code.HTTP_FAIL'));
        if (!$memberId || !is_numeric($memberId)) return JsonService::fail('系统繁忙','',config('code.HTTP_FAIL'));
        $data = Goods::getListGoodsCoupon($id,$memberId);   //查询该商品下的优惠券
        if ($data){
            return JsonService::success('查询成功',$data,config('code.HTTP_OK'));
        }else{
            return JsonService::fail('查询失败',[],config('code.HTTP_FAIL'));
        }
    }
}